package View;

import Module.Team;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer; // not java.util.Timer
import java.text.NumberFormat;
import java.net.*;

/**
 *
 * @author CCD gruppe 2
 */
class MainSessionPanel extends JPanel {

    private javax.swing.JScrollPane jScrollPane9;
    private javax.swing.JList sessionJList;
    private javax.swing.JTabbedPane sessionsTabbedPane;
    private javax.swing.JButton createSessionPlanButton;
    private ArrayList<SessionPanelForTabbedPane> sessionPanels;
    private JButton addNewSessionButton;
    private JButton removeSessionButton;
    private JButton editSessionButton;
    private javax.swing.JButton addMeButton;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton removeMeButton;
    private javax.swing.JList voiceList;
    private javax.swing.JPanel voiceListPanel;
    DefaultListModel projectModel;
    DefaultListModel sessionModel;
    DefaultComboBoxModel sessionPlaceModel;
    private DefaultListModel voiceListModel;
    private JPanel timerPanel;
    private JButton newCountdownButton;
    private JButton stopCountdownButton;
    private JLabel timerLabel;
    long remaining;
    long lastUpdate;
    Timer timer;
    NumberFormat format;

    public MainSessionPanel() {
        initComponents();
    }

    private void initComponents() {
        sessionsTabbedPane = new javax.swing.JTabbedPane();
        sessionPanels = new ArrayList<SessionPanelForTabbedPane>();
        timerPanel = new javax.swing.JPanel();
        newCountdownButton = new javax.swing.JButton();
        stopCountdownButton = new javax.swing.JButton();
        timerLabel = new JLabel();


        createSessionPlanButton = new javax.swing.JButton();
        jScrollPane9 = new javax.swing.JScrollPane();
        sessionJList = new javax.swing.JList();
        addNewSessionButton = new JButton();
        removeSessionButton = new JButton();
        editSessionButton = new JButton();
        voiceListPanel = new javax.swing.JPanel();
        addMeButton = new javax.swing.JButton();
        removeMeButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        voiceList = new javax.swing.JList();
        createSessionPlanButton.setText("Lag sesjonsplan");

        sessionsTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        jScrollPane9.setViewportView(sessionJList);


        addNewSessionButton.setText("Legg til sesjon");
        voiceListModel = new DefaultListModel();
        removeSessionButton.setText("Slett sesjon");
        editSessionButton.setText("Rediger");
        editSessionButton.setVisible(true);
        addNewSessionButton.setVisible(true);
        removeSessionButton.setVisible(true);
        sessionsTabbedPane.setVisible(false);
        voiceListPanel.setVisible(false);
        voiceList.setModel(voiceListModel);


        timerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Timer"));
        timerPanel.setVisible(false);

        newCountdownButton.setText("Ny nedtelling");
        newCountdownButton.setEnabled(false);


        stopCountdownButton.setText("Stopp nedtelling");
        stopCountdownButton.setEnabled(false);
        
        timerLabel.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N

        org.jdesktop.layout.GroupLayout timerPanelLayout = new org.jdesktop.layout.GroupLayout(timerPanel);
        timerPanel.setLayout(timerPanelLayout);
        timerPanelLayout.setHorizontalGroup(
            timerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(timerPanelLayout.createSequentialGroup()
                .add(newCountdownButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 9, Short.MAX_VALUE)
                .add(stopCountdownButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 142, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, timerPanelLayout.createSequentialGroup()
                .addContainerGap(94, Short.MAX_VALUE)
                .add(timerLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(90, 90, 90))
        );
        timerPanelLayout.setVerticalGroup(
            timerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, timerPanelLayout.createSequentialGroup()
                .add(47, 47, 47)
                .add(timerLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 55, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 70, Short.MAX_VALUE)
                .add(timerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(newCountdownButton)
                    .add(stopCountdownButton)))
        );



        voiceListPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Taleliste"));

        addMeButton.setText("Legg meg til");

        removeMeButton.setText("Fjern meg");

        jScrollPane2.setViewportView(voiceList);

        org.jdesktop.layout.GroupLayout voiceListPanelLayout = new org.jdesktop.layout.GroupLayout(voiceListPanel);
        voiceListPanel.setLayout(voiceListPanelLayout);
        voiceListPanelLayout.setHorizontalGroup(
                voiceListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(voiceListPanelLayout.createSequentialGroup().addContainerGap().add(voiceListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING).add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE).add(org.jdesktop.layout.GroupLayout.LEADING, voiceListPanelLayout.createSequentialGroup().add(addMeButton).addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED).add(removeMeButton))).addContainerGap()));
        voiceListPanelLayout.setVerticalGroup(
                voiceListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(org.jdesktop.layout.GroupLayout.TRAILING, voiceListPanelLayout.createSequentialGroup().addContainerGap().add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE).addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(voiceListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(addMeButton).add(removeMeButton)).addContainerGap()));


        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(19, 19, 19)
                        .add(addNewSessionButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(removeSessionButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(createSessionPlanButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 167, Short.MAX_VALUE)
                        .add(editSessionButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 132, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(sessionsTabbedPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(timerPanel, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(voiceListPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(sessionsTabbedPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 556, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 28, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(editSessionButton)
                            .add(addNewSessionButton)
                            .add(removeSessionButton)
                            .add(createSessionPlanButton))
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(timerPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(18, 18, 18)
                        .add(voiceListPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
        );
    }

    /**
     * A method that shows the session objects.
     */
    public void showSessionObjects() {
        sessionsTabbedPane.setVisible(true);
        createSessionPlanButton.setVisible(true);
        voiceListPanel.setVisible(true);
    }

    /**
     * A method that removes all session panels.
     */
    public void clearSessionPanels() {
        sessionsTabbedPane.removeAll();
        sessionPanels.clear();
    }

    /**
     * A method that adds a session panel.
     */
    public void addSessionPanel() {
        SessionPanelForTabbedPane newSessionPanel = new SessionPanelForTabbedPane();
        sessionsTabbedPane.setVisible(true);
        newSessionPanel.editButtonClicked();
        sessionPanels.add(newSessionPanel);
        sessionsTabbedPane.add("Sesjon " + sessionPanels.size(), newSessionPanel);

    }

    /**
     * A method that removes a session panel.
     * @param sessionPanel
     */
    public void removeSessionPanel(SessionPanelForTabbedPane sessionPanel) {
        for (int i = 0; i < sessionPanels.size(); i++) {
            if (sessionPanel.equals(sessionsTabbedPane.getComponentAt(i))) {
                sessionsTabbedPane.remove(i);
                break;
            }
        }
        sessionPanels.remove(sessionPanel);

    }

    /**
     * A method that starts the countdown timer.
     * @param time
     */
    public void countdown(int time){
        remaining = time*60000;
        timerLabel.setHorizontalAlignment(SwingConstants.CENTER);
        timerLabel.setOpaque(true); // So label draws the background color
        format = NumberFormat.getNumberInstance();
        format.setMinimumIntegerDigits(2);
        timer = new Timer(1000, new countdown());
        timer.setInitialDelay(0);
        resume();

    }

    /**
     * A method that stops the countdown timer.
     */
    public void stopCountdown(){
        timer.stop();
    }

    /**
     * A method that resumes the countdown timer.
     */
    public void resumeCountdown(){
        resume();
    }

    /**
     * A method that updates the countdown display.
     */
    public void upDateDisplay(){
        long now = System.currentTimeMillis(); // current time in ms
            long elapsed = now - lastUpdate; // ms elapsed since last update
            remaining -= elapsed; // adjust remaining time
            lastUpdate = now; // remember this update time
            if (remaining < 0) {
                remaining = 0;
            }
            int minutes = (int) (remaining / 60000);
            int seconds = (int) ((remaining % 60000) / 1000);
            timerLabel.setText(format.format(minutes) + ":" + format.format(seconds));
            if (remaining == 0) {
            }
    }

    /**
     * A method that resumes the countdown timer.
     */
    public void resume() {
            lastUpdate = System.currentTimeMillis();
            timer.start(); // Start the timer
        }

    private class countdown implements ActionListener{

        public void actionPerformed(ActionEvent ae) {
            upDateDisplay();
        }

    }

    /*
     * Getters and setters
     */

    public SessionPanelForTabbedPane getSessionPanelForTab(int index) {
        if (!sessionPanels.isEmpty()) {
            return sessionPanels.get(index);
        } else {
            return null;
        }

    }
    public ArrayList<SessionPanelForTabbedPane> getSessionPanels() {
        return sessionPanels;
    }

    public JButton getCreateSessionPlanButton() {
        return createSessionPlanButton;
    }

    public JScrollPane getjScrollPane9() {
        return jScrollPane9;
    }

    public DefaultListModel getProjectModel() {
        return projectModel;
    }

    public JList getSessionJList() {
        return sessionJList;
    }

    public DefaultListModel getSessionModel() {
        return sessionModel;
    }

    public DefaultComboBoxModel getSessionPlaceModel() {
        return sessionPlaceModel;
    }

    public JTabbedPane getSessionsTabbedPane() {
        return sessionsTabbedPane;
    }

    public SessionPanelForTabbedPane getSelectedSessionPanel() {
        return sessionPanels.get(sessionsTabbedPane.getSelectedIndex());
    }

    public void setCreateSessionPlanButton(JButton createSessionPlanButton) {
        this.createSessionPlanButton = createSessionPlanButton;
    }

    public void setjScrollPane9(JScrollPane jScrollPane9) {
        this.jScrollPane9 = jScrollPane9;
    }

    public void setProjectModel(DefaultListModel projectModel) {
        this.projectModel = projectModel;
    }

    public void setSessionJList(JList sessionJList) {
        this.sessionJList = sessionJList;
    }

    public void setSessionModel(DefaultListModel sessionModel) {
        this.sessionModel = sessionModel;
    }

    public void setSessionPanelForTabbedPane(ArrayList<SessionPanelForTabbedPane> sessionPanels) {
        this.sessionPanels = sessionPanels;
    }

    public void setSessionPlaceModel(DefaultComboBoxModel sessionPlaceModel) {
        this.sessionPlaceModel = sessionPlaceModel;
    }

    public void setSessionsTabbedPane(JTabbedPane sessionsTabbedPane) {
        this.sessionsTabbedPane = sessionsTabbedPane;
    }

    public JButton getRemoveSessionButton() {
        return removeSessionButton;
    }

    public JButton getAddNewSessionButton() {
        return addNewSessionButton;
    }

    public JButton getEditSessionButton() {
        return editSessionButton;
    }

    public JButton getAddMeButton() {
        return addMeButton;
    }

    public JScrollPane getjScrollPane2() {
        return jScrollPane2;
    }

    public JButton getRemoveMeButton() {
        return removeMeButton;
    }

    public JList getVoiceList() {
        return voiceList;
    }

    public JPanel getVoiceListPanel() {
        return voiceListPanel;
    }

    public DefaultListModel getVoiceListModel() {
        return voiceListModel;
    }

    public JLabel getTimerLabel() {
        return timerLabel;
    }

    public JButton getNewCountdownButton() {
        return newCountdownButton;
    }

    public JButton getStopCountdownButton() {
        return stopCountdownButton;
    }

    public JPanel getTimerPanel() {
        return timerPanel;
    }
}
